package com.net.oil.coupon.fragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseFragment;
import com.net.oil.coupon.adapter.CouponListAdapter;
import com.net.oil.coupon.bean.CouponList;
import com.net.oil.coupon.contract.CouponListContract;
import com.net.oil.coupon.presenter.CouponListPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CouponListFragment extends BaseFragment<CouponListPresenter> implements CouponListContract.View {

    @BindView(R.id.coupon_list)
    RecyclerView couponList;
    Unbinder unbinder;

    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;
    private int used = -2;          //使用状态(1:已使用 0：未使用 -1：已过期)

    private CouponListAdapter adapter;

    public static CouponListFragment getInstance(int position) {
        CouponListFragment fragment = new CouponListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.COUPON_SELECT_POSITION, position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            int position = bundle.getInt(Constant.COUPON_SELECT_POSITION, -1);
            if (position == 0) {
                used = 0;
            }
            if (position == 1) {
                used = 1;
            }
            if (position == 2) {
                used = -1;
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_coupon_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        presenter = new CouponListPresenter(this);
        initView();
        initData();
        return view;
    }

    private void initData() {
        if (used != -2) {
            showProgressDialog();
            presenter.getCouponList(used, page, rows);
        }
    }

    private void initView() {

        adapter = new CouponListAdapter(getContext(), used);
        couponList.setLayoutManager(new LinearLayoutManager(getContext()));
        couponList.setAdapter(adapter);

        couponList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //如果不能向上滚动，有更多则加载更多
                if (!couponList.canScrollVertically(1) && haveMore && page > 1) {
                    adapter.changeMoreStatus(Constant.LOADING_MORE);
                    presenter.getCouponList(used, page, rows);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 获取优惠券列表成功
     *
     * @param couponBeans 优惠券列表数据
     */
    @Override
    public void getCouponListSuccess(CouponList couponBeans) {
        dismissProgressDialog();
        if (couponBeans != null) {
            if (couponBeans.getList() != null && couponBeans.getList().size() > 0) {
                if (page == 1){
                    adapter.updateData(couponBeans.getList());
                }else if (page > 1){
                    adapter.loadMore(couponBeans.getList());
                }
                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;
            }else {
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }
        }
    }

    /**
     * 获取优惠券列表失败
     */
    @Override
    public void getCouponListFailed() {
        dismissProgressDialog();
    }
}
